package com.mc.moring.util

import android.text.InputFilter
import android.text.Spanned
import android.widget.EditText
import java.util.regex.Pattern

object MyTextUtils {

    fun hasChinese(input: String?): Boolean {
        val regex = "[\u4e00-\u9fa5]"
        return Pattern.matches(".*$regex.*", input)
    }

    fun setLengthLimit(editText: EditText, min: Int, max: Int) {
        val filters = arrayOf<InputFilter>(LengthFilter(min, max))
        editText.filters = filters
    }

    fun verificationPwd(password: String): Boolean {
        if (password.length > 20 || password.length < 4) {
            return false
        }
        val pattern = Pattern.compile("[a-zA-Z0-9\\S]+")//改为可以输入字符
        val matcher = pattern.matcher(password)
        return matcher.matches()
    }

    fun verificationPwdLength(password: String): Boolean {
        return password.length >= 4
    }

    class LengthFilter(private val minLength: Int, private val maxLength: Int) : InputFilter {
        override fun filter(
            source: CharSequence,
            start: Int,
            end: Int,
            dest: Spanned,
            dstart: Int,
            dend: Int
        ): CharSequence? {

            var keep = maxLength - (dest.length - (dend - dstart))
            return if (keep <= 0) {
                ""
            } else if (keep >= end - start) {
                null
            } else {
                keep += start
                if (Character.isHighSurrogate(source[keep - 1])) {
                    --keep
                    if (keep == start) {
                        return ""
                    }
                }
                source.subSequence(start, keep)
            }
        }
    }


}